package com.cxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxt.Mapper.AccountInfoMapper;
import com.cxt.context.BaseContext;
import com.cxt.entity.AccountInfo;
import com.cxt.entity.BillInfo;
import com.cxt.service.IAccountInfoService;
import com.cxt.service.IBillInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements IAccountInfoService {
    @Autowired
    private IBillInfoService billInfoService;
    @Override
    @Transactional
    public Boolean up(Long accountId, BigDecimal paymentAmount, int i, String msg) {
        //添加一条入出账单
        BillInfo  billInfo = BillInfo.builder()
                .accountId(accountId)
                .amount(paymentAmount)
                .type(i)
                .description(msg)
                .transactionTime(LocalDateTime.now())
                .build();
        boolean isAdd = billInfoService.save(billInfo);
        //修改账户余额
        AccountInfo accountInfo = getById(accountId);
        if(i==1){
            accountInfo.setCurrentBalance(accountInfo.getCurrentBalance().add(paymentAmount));
        }else if(i==2) {
            accountInfo.setCurrentBalance(accountInfo.getCurrentBalance().subtract(paymentAmount));
            if(accountInfo.getCurrentBalance().compareTo(BigDecimal.ZERO)<0){
                throw new RuntimeException("账户余额不足");
            }
        }
        boolean b = updateById(accountInfo);
        return isAdd&&b;
    }

    @Override
    public void addAccountInfo(Long userId) {
        AccountInfo accountInfo = AccountInfo.builder()
                .driverId(userId)
                .bankCardNumber(null)
                .bankName(null)
                .currentBalance(BigDecimal.ZERO)
                .build();
        save(accountInfo);
    }

    @Override
    public boolean updateAccountIfo(AccountInfo accountInfo) {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AccountInfo> query = new LambdaQueryWrapper<>();
        query.eq(AccountInfo::getDriverId,currentId);
        AccountInfo accountInfo1 = getOne(query);
        accountInfo.setId(accountInfo1.getId());
        accountInfo.setDriverId(currentId);
        return updateById(accountInfo);
    }

    @Override
    public AccountInfo getAccountInfo() {
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<AccountInfo> query = new LambdaQueryWrapper<>();
        query.eq(AccountInfo::getDriverId,id);
        return getOne(query);
    }

    @Override
    public boolean withdraw(Double deduction) {
        LambdaQueryWrapper<AccountInfo> query = new LambdaQueryWrapper<>();
        query.eq(AccountInfo::getDriverId,BaseContext.getCurrentId());
        AccountInfo accountInfo = getOne(query);
        Boolean isAdd = this.up(accountInfo.getId(), BigDecimal.valueOf(deduction), 2, "提现");
        return isAdd;
    }

    @Override
    public List<BillInfo> getBillInfo() {
        Long  id = BaseContext.getCurrentId();
        LambdaQueryWrapper<AccountInfo> query = new LambdaQueryWrapper<>();
        query.eq(AccountInfo::getDriverId,id);
        AccountInfo accountInfo = getOne(query);
        LambdaQueryWrapper<BillInfo> query1 = new LambdaQueryWrapper<>();
        query1.eq(BillInfo::getAccountId,accountInfo.getId());
        return billInfoService.list(query1);
    }
}
